package com.apes.fa.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.scm.stock.transfer.model
 * @ClassName: InventoryTransferAsset
 * @Author: pyb
 * @Description: 库存转资产
 * @CreateTime: 2021-04-23 14:30
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InventoryTransferAsset.CONST_TABLE_NAME)
public class InventoryTransferAsset extends Model {
    public static final String CONST_TABLE_NAME = "inventory_transfer_asset";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "库存转资产", prefix = "KZ%(company)", padding = 5)
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门")
    private Dept dept;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("仓库")
    private Location location;

    @Comment("业务员")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "clerk_id", referencedColumnName = "id")
    private PartyRole clerk;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "sent", name = "新建"),
            @State(id = "approve", name = "审核"),
            @State(id = "done", name = "已完成")})
    private String state;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "date_approve")
    @Comment("审批日期")
    private Date dateApprove;

    @Column(name = "note", length = 1000)
    @Comment("备注")
    private String note;

    @OneToMany(mappedBy = "inventoryTransferAsset", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<InventoryTransferAssetItem> inventoryTransferAssetItems = new ArrayList<>();

    public List<InventoryTransferAssetItem> getInventoryTransferAssetItems() {
        for (InventoryTransferAssetItem inventoryTransferAssetItem : inventoryTransferAssetItems) {
            inventoryTransferAssetItem.setInventoryTransferAsset(this);
        }
        return inventoryTransferAssetItems;
    }
}
